package com.smartthinking.dive;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Locale;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.graphics.Bitmap;

public class handXML extends DefaultHandler {

	private Dictionary<String, String> dictionary;

	private StringBuilder sbItem;
	Bitmap image;

	private ArrayList<Dictionary<String, String>> dXML;

	private Enumeration Keys_;

	private Enumeration enuma;

	private String test;

	// DownloadImagesTask task1;
	//
	// <CIUDAD>
	// <ID_CIUDAD>1</ID_CIUDAD>
	// <NOM_CIUDAD>M�xico</NOM_CIUDAD>
	// <ID_ESTADO>9</ID_ESTADO>
	// <ESTADO>Distrito Federal</ESTADO>
	// </CIUDAD>

	@SuppressWarnings("unchecked")
	public ArrayList<Dictionary<String, String>> getXML() {

		String d = Locale.getDefault().getDisplayLanguage();

		if (!d.toUpperCase().equals("ESPAÑOL")) {

			// Registro
			for (int x = 0; x < dXML.size(); x++)

			{
				// Datos
				enuma = dXML.get(x).keys();

				while (enuma.hasMoreElements()) {
					String value = (String) enuma.nextElement();

					String right = value.substring(value.length() - 3,
							value.length());

					if (right.equals("_en"))

					{
						
						
						String putObject=value.substring(0,value.length() - 3);
						
						dXML.get(x).put(putObject , dXML.get(x).get(value).toString());
							
			 

					}

				}

			}

		}
		return dXML;
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		super.characters(ch, start, length);

		if (this.dictionary != null)
			sbItem.append(ch, start, length);
	}

	@Override
	public void endElement(String uri, String localName, String name)
			throws SAXException {

		super.endElement(uri, localName, name);

		String d = Locale.getDefault().getDisplayLanguage();

		// if (d.toUpperCase().equals("ESPA�OL")) {

		if (this.dictionary != null) {
			if (!localName.equals("ROW")) {

				dictionary.put(localName, sbItem.toString());

			} else {

				dXML.add(dictionary);
			}

			sbItem.setLength(0);
		}
		// } else {
		// if (this.dictionary != null) {
		// if (!localName.equals("ROW")) {
		//
		// dictionary.put(localName, sbItem.toString());
		//
		// } else {
		//
		// for (Enumeration e = dictionary.keys(); e.hasMoreElements();) {
		// // System.out.println(e.nextElement());
		//
		// String k = e.nextElement().toString();
		//
		// // dictionary.
		//
		// }
		//
		// dXML.add(dictionary);
		//
		// }
		//
		// sbItem.setLength(0);
		// }
		//
		// }

	}

	@Override
	public void startDocument() throws SAXException {

		super.startDocument();

		dXML = new ArrayList<Dictionary<String, String>>();

		sbItem = new StringBuilder();

	}

	@Override
	public void startElement(String uri, String localName, String name,
			Attributes attributes) throws SAXException {

		super.startElement(uri, localName, name, attributes);

		if (localName.equals("ROW")) {
			dictionary = new Hashtable();
		}
	}

}
